iT邦幫忙

2024 iThome 鐵人賽

DAY 24
1
DevOps

菜逼八用Github Actions系列 第 24

Day 24 - 例子 - 發版後自動更新Jira release note狀態

  • 分享至 

  • xImage
  •  

目錄

摘要

在上一篇我們學寫了一個根據tag來deploy到Github Pages的workflow

這篇我們會來會利用Jira API來寫一個自動更新Jira release note狀態的workflow

步驟

前置作業

  1. 準備一個有內容的repo
  2. 註冊Jira帳號,並建立一個專案、release note
  3. 建立Jira token,並為repo的secret
    其餘的Jira資訊視個人需求設成secret,不設也行

撰寫撈versionId的JS actions

在.github/actions下建立get-jira-project-version資料夾,然後建一個index.js

import core from '@actions/core';

const jiraDomain = core.getInput('jiraDomain');
const jiraToken = core.getInput('jiraToken');
const projectKey = core.getInput('projectKey');
const versionName = core.getInput('versionName');

const getAllVersions = async () => {
  await fetch(`https://${jiraDomain}.atlassian.net/rest/api/3/project/${projectKey}/versions`, {
    method: 'GET',
    headers: {
      'Authorization': `Basic ${new Buffer.from(
        jiraToken
      ).toString('base64')}`,
      'Accept': 'application/json'
    }
  })
    .then(response => {
      console.log(
        `Response: ${response.status} ${response.statusText}`
      );

      return response.json();
    })
    .then((text) => {
      // 不需要在metadata file特別定義outputs,在這裡設置就好
      core.setOutput('versionId', text.find(({name}) => name === versionName)?.id);
    })
    .catch(err => console.error(err));
}

await getAllVersions();

撰寫更新release note的JS actions

在.github/actions下建立update-release-note資料夾,然後建一個index.js

import core from '@actions/core';

const today = new Date();
const releaseDate = `${today.getFullYear()}-${today.getMonth()+1}-${today.getDate()}`

const versionId = core.getInput('versionId');
const projectId = core.getInput('projectId');
const jiraToken = core.getInput('jiraToken');
const jiraDomain = core.getInput('jiraDomain');

const bodyData = `{
  "id": "${versionId}",
  "projectId": "${projectId}",
  "releaseDate": "${releaseDate}",
  "released": true
}`;

// node環境底下也可以不包在function中,直接在top level直接await fetch
await fetch(`https://${jiraDomain}.atlassian.net/rest/api/3/version/${versionId}`, {
  method: 'PUT',
  headers: {
    'Authorization': `Basic ${Buffer.from(
      jiraToken
    ).toString('base64')}`,
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  },
  body: bodyData
})
  .then(response => {
    return response.text();
  })
  .then(text => console.log(text))
  .catch(err => console.error(err));

撰寫metadata file

因為我們有兩個JS action,所以也要建立兩個action.yml,放在跟同一組的JS action同個資料夾

當然不放在同個資料夾也是可以的,只要metadata file中main屬性指向的路徑正確即可

name: Get id of release version
inputs:
  jiraDomain:
    required: true
  jiraToken:
    required: true
  projectKey:
    required: true
  versionName:
    required: true
  
runs:
  using: node20
  main: './index.js'
name: Update Jira release note
inputs:
  jiraDomain:
    required: true
  jiraToken:
    required: true
  versionId:
    required: true
  projectId:
    required: true

runs:
  using: node20
  main: './index.js'

完成這一步以後我們的檔案旁放位置如下

https://ithelp.ithome.com.tw/upload/images/20240924/20135568zEhNWLgF9b.png

在workflow中使用actions

修改一下上一篇寫的workflow,並在當中使用上一個步驟建立的actions

name: Deploy to github page

on:
  push:
    tags:
      # tag的規則視需求更改
      - 'trial@\d+.\d+.\d+'
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: ./src
          name: github-pages
      
  deploy:
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: github-pages
      url: https://<使用者名>.github.io/<repo名>/
    permissions:
      pages: write
      id-token: write
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

  # 新增job
  update-jira-release-note:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repo
        uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
      - run: |
          npm install
          npm list
        shell: bash
      
      - name: get id of release version
        id: get_version
        uses: ./.github/actions/get-jira-project-version
        with:
          jiraDomain: ${{secrets.JIRA_DOMAIN}}
          jiraToken: ${{secrets.JIRA_TOKEN}}
          projectKey: TGA
          versionName: ${{github.ref_name}}


      - name: update jira release note
        uses: ./.github/actions/update-release-note
        with:
          jiraDomain: ${{secrets.JIRA_DOMAIN}}
          jiraToken: ${{secrets.JIRA_TOKEN}}
          versionId:  ${{steps.get_version.outputs.versionId}}
          projectId: 你的project的id

完成後會像這樣

https://ithelp.ithome.com.tw/upload/images/20240928/201355680Lu2ZLfJbI.png


上一篇
Day 23 - 例子 - 把檔案上傳到Github Pages
下一篇
Day 25 - 例子 - 完成發版後發送通知
系列文
菜逼八用Github Actions30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言